/**
 * Copyright (c) 2024 - CARThunder
 * All Rights Reserved by Chengdu CARThunder Intelligent Technology Co., LTD and its affiliates.
 * You may not use, copy, distribute, modify, transmit in any form this file
 * except in compliance with CARThunder in writing by applicable law.
 *
 */

/**
 * file description.
 * @file    filename
 * @brief   brief function description.
 * @details detailed function description.
 * @version 1.0
 * @author  author_name
 * @date    last_modified_date
 *
 *      Edit History
 *      ----------------------------------------------------------------------------
 *      DATE                     NAME               DESCRIPTION
 *      2024-08-2               jia fengyi         Create it.
 *
 */

package com.cart.musicdemo.fragment;

import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cart.musicdemo.MainActivity;
import com.cart.musicdemo.R;
import com.cart.musicdemo.adapter.ArtistSongListAdapter;
import com.cart.musicdemo.base.service.MusicService;
import com.cart.musicdemo.model.SongModel;
import com.cart.musicdemo.model.UpdateEventUI;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class ArtistSongListFragment extends Fragment {


    private RecyclerView recyclerView;

    private MusicService.MusicBinder musicService;

    private List<SongModel> songList;

    private ArtistSongListAdapter artistSongListAdapter;

    private ImageView imageBack;

    private ArtistSongListFragment fragment;


    public ArtistSongListFragment() {
        // Required empty public constructor
    }

    public ArtistSongListFragment(List<SongModel> songs,
                                  MusicService.MusicBinder musicService) {
        fragment = new ArtistSongListFragment();
        songList = songs;
        this.musicService = musicService;
        Bundle args = new Bundle();
        //传参
        args.putBinder("musicService", musicService);
        args.putParcelableArrayList("songs", (ArrayList<? extends Parcelable>) songs);
        fragment.setArguments(args);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        if (getArguments() != null) {
            songList = getArguments().getParcelableArrayList("songs");
            musicService = (MusicService.MusicBinder) getArguments()
                    .getBinder("musicService");
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onUpdateEventUI(UpdateEventUI event) {
        String type = event.getType();
        if (type.equals("clearUI")) {
            //拔出USB时清空数据
            songList.clear();
        }
        // 更新 UI
        artistSongListAdapter.notifyDataSetChanged();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_artisit_song_list,
                container, false);
        imageBack = view.findViewById(R.id.tv_artist_back);
        imageBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getFragmentManager().popBackStack();
            }
        });
        recyclerView = view.findViewById(R.id.rv_song_list_RecyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        artistSongListAdapter  = new ArtistSongListAdapter(songList, this::onItemClick);
        recyclerView.setAdapter(artistSongListAdapter);
        return view;
    }

    private void onItemClick(SongModel songModel, boolean reset) {
        //更新MusicService播放列表数据源
        musicService.updateSongList(songList);
        int position = musicService.getSonglist().indexOf(songModel);
        musicService.setCurrentPosition(position);
        //更新播放列表
        musicService.play(position, true);
        MainActivity activity = (MainActivity) getActivity();
        if (activity != null) {
            activity.updateStartButtonStatus();
        }
        //当前UI更新
        artistSongListAdapter.notifyDataSetChanged();
        //播放列表UI更新
        musicService.updatePlayListUI();
    }


}

